
const pdfjsLib = require('pdfjs-dist/build/pdf')
const workerSrc = require('pdfjs-dist/build/pdf.worker.entry')
pdfjsLib.GlobalWorkerOptions.workerSrc = workerSrc
const renderPDFToImages = async (pdfURL, imageLink)=> {
    const pdf = await pdfjsLib.getDocument(pdfURL).promise;
    // 遍历所有页面，渲染为 PNG
    for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
        const page = await pdf.getPage(pageNum);
        const scale = 1.5;
        const viewport = page.getViewport({ scale: scale });

        const canvas = document.createElement('canvas');
        const context = canvas.getContext('2d');
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        const renderContext = {
            canvasContext: context,
            viewport: viewport,
        };

        // 渲染页面到 canvas
        await page.render(renderContext).promise;

        // 将 canvas 转为 PNG 数据 URL
        const imageURL = canvas.toDataURL('image/png');
        console.log("===>imageRUL", imageURL)
        imageLink.push(imageURL); // 将生成的 PNG 链接添加到数组
    }
}


export default renderPDFToImages